#include "ExpAST.h"
#include "Globe.h"
#include <iostream>
#include "parser.h"
#include "Globe.h"
#pragma warning(disable:4267)
using std::cout;
using std::endl;

unique_ptr<BaseExpAST> parser::parseTernaryExp()
{
    unique_ptr<BaseExpAST>l = this->parseLOrExp();
    if(this->lexer->next_token()==TOK::QUESTION)
    {
        this->lexer->match(TOK::QUESTION);
        unique_ptr<TernaryAST>t(new TernaryAST);
        t->content = std::move(l);
        t->exp1 = this->parseLOrExp();
        this->lexer->match(TOK::COLON);
        t->exp2 = this->parseLOrExp();
        return t;
    }
    return l;
}

void TernaryAST::Dump() const
{
    cout << "{\"Ternary\":\"Add\",\"condition\":";
    this->content->Dump();
    cout << ",\"exp1\":";
    this->exp1->Dump();
    cout << ",\"exp2\":";
    this->exp2->Dump();
    cout << "}";
}
bool TernaryAST::is_lval() const
{
    return false;
}

void TernaryAST::Analyze() const
{
    this->content->Analyze();
    GENASM(cmd_code::CMP, CVR(EAX), CVI(0));
    size_t jump_tag_0 = ++jump_tag;
    GENASM(cmd_code::JE, CVI(jump_tag_0), cmd_val());
    this->exp1->Analyze();
    size_t jump_tag_1 = ++jump_tag;
    GENASM(cmd_code::JMP, CVI(jump_tag_1), cmd_val());
    GENASM(cmd_code::FLAG, CVI(jump_tag_0), cmd_val());
    this->exp2->Analyze();
    GENASM(cmd_code::FLAG, CVI(jump_tag_1), cmd_val());
}

